import { Injectable } from '@nestjs/common';
import { TypeOrmCrudService } from '@nestjsx/crud-typeorm';
import { Users } from './systems/users/users.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { AuthService  } from './systems/auth/auth.service';
import { LogService } from './systems/log/log.service';
@Injectable()
export class AppService extends TypeOrmCrudService<Users> {
  constructor(
    @InjectRepository(Users)
    repo: Repository<Users>,
    private readonly authService:AuthService,
    private readonly logService:LogService

  ) {
    super(repo);
  }
  async login(userName: string, password: string,ip:string) {
    //生成token
    let token=await this.authService.createToken(userName,password);
    const filter: any = [
      {
        userName: userName,
        password: password,
      },
      {
        account: userName,
        password: password,
      },
     
    ];
    let user = await this.repo.find({
      where: filter,
    });
    let nowDate=new Date();
    if(user.length>0){
      let logInfo={
        account:userName,
        loginTime: nowDate.toLocaleString(),
        ip:ip
      }
      this.logService.addLog(logInfo);
      let data = {
        msg:'ok',
        token:token,
        user:{
          account: user[0].account,
          phone: user[0].phone,
          userName: user[0].userName,
          position: user[0].position,
        }
      };
      return  data;
    }else{
      let data={
        code:0,
        msg:'error'
      }
      return data;
    }
    
  }
  
}
